package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.FileUtils;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.Task;
import cn.stylefeng.guns.sys.modular.system.entity.TaskFlow;
import cn.stylefeng.guns.sys.modular.system.entity.TaskStatus;
import cn.stylefeng.guns.sys.modular.system.model.params.TaskParam;
import cn.stylefeng.guns.sys.modular.system.service.TaskService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author jiangge
 * @Date 2020-09-10 22:11:50
 */
@Controller
@RequestMapping("/task")
public class TaskController extends BaseController {

    private String PREFIX = "/modular/system/task";

    @Autowired
    private TaskService taskService;

    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/task.html";
    }

    /**
     * 督导开发任务页面
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/steerTask")
    public String steerTask(Integer id, ModelMap map) {
        map.put("list", taskList(id));
        return PREFIX + "/steerTask.html";
    }

    /**
     * 工程开发任务页面
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/projectTask")
    public String projectTask(Integer proId, ModelMap map) {
        map.put("list", taskList(proId));
        return PREFIX + "/projectTask.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/task_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TaskParam taskParam) {
        this.taskService.add(taskParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TaskParam taskParam) {
        this.taskService.update(taskParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TaskParam taskParam) {
        this.taskService.delete(taskParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TaskParam taskParam) {
        Task detail = this.taskService.getById(taskParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TaskParam taskParam) {
        return this.taskService.findPageBySpec(taskParam);
    }


    /**
     * 新增任务信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRent")
    //@PostMapping("/addRent")
    public ResponseData addRent(@RequestBody List<TaskStatus> taskStatus) throws ParseException {
        taskService.addRent(taskStatus);
        return ResponseData.success();
    }

    @ResponseBody
    @PostMapping("/aaa")
    public ResponseData aaa(@RequestBody List<TaskStatus> taskStatus) throws ParseException {
        taskService.aaa(taskStatus);
        return ResponseData.success();
    }


    /**
     * 上传图片
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/uploads")
    //@PostMapping("/uploads")
    public Map<String, Object> uploads(@RequestPart("file") MultipartFile file, Integer proId, Integer taskId) {

        Map<String, Object> msg = new HashMap<>(16);
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //图片名称为uuid+图片后缀防止冲突
            String fileName = UUIDUtils.getUUID() + "." + suffix;
            //获取操作系统名称
            String os = System.getProperty("os.name");
            //文件保存路径
            String filePath = "";
            // filePath=ConstantsContext.getFileUploadPath();
            if (os.toLowerCase().startsWith("win")) {
                //windows下的路径
                filePath = "D:/pictureUpload/project/gc/";
            } else {
                //linux下的路径
                filePath = "/root/pictureUpload/project/gc/";
            }
            //写入图片
            Boolean writePictureflag = FileUtils.uploadFile(file.getBytes(), filePath, fileName);
            //上传成功后，将可以访问的完整路径返回
            String fullImgpath = "/pictureUpload/project/gc/" + fileName;
            //String fullImgpath = filePath+fileName;
            Map<String, Object> data = new HashMap<>(16);
            //0表示成功，1失败
            msg.put("code", 0);
            msg.put("msg", "上传成功");
            msg.put("data", data);
            data.put("src", fullImgpath);
            data.put("title", StringUtils.substringAfterLast(fullImgpath, "/"));

            //状态表实体
            TaskStatus status = new TaskStatus();
            status.setProId(proId);
            status.setTaskId(taskId);
            status.setStatus(1);
            status.setContent(fullImgpath);

            //流程明细表
            TaskFlow taskFlow = new TaskFlow();
            taskFlow.setProId(status.getProId());
            taskFlow.setTaskId(status.getTaskId());
            taskFlow.setStatus(1);//1提交；2审核未通过；3审核通过
            if (status.getTaskName() != null || status.getTaskName() != "") {
                taskFlow.setTaskName(status.getTaskName());
            }
//                        taskFlow.setCreateUser(LoginContextHolder.getContext().getUserId());
//                        taskFlow.setCreateTime(new Date());

            //状态表新增数据
            taskService.insertRent(status, taskFlow);
        } catch (Exception e) {
            e.printStackTrace();
            msg.put("code", 1);
            msg.put("msg", "上传失败");
        }
        return msg;
    }

    /**
     * 督导/工程部任务数据
     */
    public String taskList(Integer proId) {
        ArrayList<TaskStatus> taskStatuses = (ArrayList) taskService.taskList(proId);
        String listJson = JSONArray.toJSONString(new ArrayList[]{taskStatuses});
        return listJson;
    }


}


